<?php
defined('IN_ADMIN') or exit('No permission resources.');
include $this->admin_tpl('header','admin');
?>
<link href="<?php echo CSS_PATH?>cropper.min.css" rel="stylesheet" type="text/css" />
<link href="<?php echo CSS_PATH?>ImgCropping.css" rel="stylesheet" type="text/css" />
<script language="javascript" type="text/javascript" src="<?php echo JS_PATH?>cropper.min.js"></script>
<style type="text/css">
.tailoring-container .tailoring-content {width: 780px;height: 520px;box-shadow: 0 0 0 #000; top:10px;}
</style>
</head>
<body>
<div class="tailoring-container">
    <!-- <div class="black-cloth" onclick="closeTailor(this)"></div> -->
    <div class="tailoring-content">
        <!-- <div class="tailoring-content-one">
            <label title="上传图片" for="chooseImg" class="l-btn choose-btn">
                <input type="file" accept="image/*" name="file" id="chooseImg" class="hidden" onchange="selectImg(this)">
                选择图片
            </label>
            <div class="close-tailoring"  onclick="closeTailor(this)">×</div>
        </div> -->
        <div class="tailoring-content-two">
            <div class="tailoring-box-parcel">
                <!-- <img id="tailoringImg"> -->
                <img src="<?php echo $picurl?>" alt="<?php echo $picurl?>" id="tailoringImg"  width="100%">
            </div>
            <div class="preview-box-parcel">
                <p>图片预览：</p>
                <!-- <div class="square previewImg"></div> -->
                <div class="previewImg-box">
                    <div class="previewImg slg"></div>
                    <!-- <p>180*180</p> -->
                </div>
                <!-- <div class="previewImg-box">
                    <div class="previewImg lg"></div>
                    <p>90*90</p>
                </div>
                <div class="previewImg-box">
                    <div class="previewImg md"></div>
                    <p>45*45</p>
                </div>
                <div class="previewImg-box">
                    <div class="previewImg sm"></div>
                    <p>30*30</p>
                </div> -->
            </div>
        </div>
        <div class="tailoring-content-three">
            <button class="l-btn cropper-reset-btn">复位</button>
            <button class="l-btn cropper-rotate-btn">旋转</button>
            <button class="l-btn cropper-scaleX-btn">换向</button>
            <span style="display: inline-block;">
				宽：<input name="dataWidth" id="dataWidth" value="200" style="width:60px;">
            </span>
            <span style="display: inline-block;">
				高：<input name="dataHeight" id="dataHeight" value="200" style="width:60px;">
            </span>
            <button class="l-btn sureCut" id="sureCut">裁剪</button>
        </div>
    </div>
</div>
<script>
    //弹出框水平垂直居中
    (window.onresize = function () {
        var win_height = $(window).height();
        var win_width = $(window).width();
        if (win_width <= 768){
            $(".tailoring-content").css({
                "top": (win_height - $(".tailoring-content").outerHeight())/2,
                "left": 0
            });
        }else{
            $(".tailoring-content").css({
                "top": (win_height - $(".tailoring-content").outerHeight())/2,
                "left": (win_width - $(".tailoring-content").outerWidth())/2
            });
        }
    })();

    //弹出图片裁剪框
    $("#replaceImg,#finalImg").on("click",function () {
        $(".tailoring-container").toggle();
    });
    //图像上传
    function selectImg(file) {
        if (!file.files || !file.files[0]){
            return;
        }
        var reader = new FileReader();
        reader.onload = function (evt) {
            var replaceSrc = evt.target.result;
            //更换cropper的图片
            $('#tailoringImg').cropper('replace', replaceSrc,false);//默认false，适应高度，不失真
        }
        reader.readAsDataURL(file.files[0]);
    }
    //cropper图片裁剪
	var $dataHeight = $('#dataHeight');
	var $dataWidth = $('#dataWidth');
    $('#tailoringImg').cropper({
        // aspectRatio: 1/1,//默认比例
        preview: '.previewImg',//预览视图
        guides: true,  //裁剪框的虚线(九宫格)
        autoCropArea: 0.9,  //0-1之间的数值，定义自动剪裁区域的大小，默认0.8
        movable: false, //是否允许移动图片
        dragCrop: true,  //是否允许移除当前的剪裁框，并通过拖动来新建一个剪裁框区域
        movable: true,  //是否允许移动剪裁框
        resizable: true,  //是否允许改变裁剪框的大小
        zoomable: true,  //是否允许缩放图片大小
        mouseWheelZoom: false,  //是否允许通过鼠标滚轮来缩放图片
        touchDragZoom: true,  //是否允许通过触摸移动来缩放图片
        rotatable: true,  //是否允许旋转图片
        crop: function(e) {
            // 输出结果数据裁剪图像。
            $('#dataHeight').val(Math.round(e.height));
  			$('#dataWidth').val(Math.round(e.width));
        }
    });
    //旋转
    $(".cropper-rotate-btn").on("click",function () {
        $('#tailoringImg').cropper("rotate", 45);
    });
    //复位
    $(".cropper-reset-btn").on("click",function () {
        $('#tailoringImg').cropper("reset");
    });
    //换向
    var flagX = true;
    $(".cropper-scaleX-btn").on("click",function () {
        if(flagX){
            $('#tailoringImg').cropper("scaleX", -1);
            flagX = false;
        }else{
            $('#tailoringImg').cropper("scaleX", 1);
            flagX = true;
        }
        flagX != flagX;
    });

    //裁剪后的处理
    $("#sureCut").on("click",function () {
        if ($("#tailoringImg").attr("src") == null ){
            return false;
        }else{
            // $('#tailoringImg').cropper('setCanvasData',{width:620,height:400});
            // $('#tailoringImg').cropper('setData',{width:620,height:400});
            var cas = $('#tailoringImg').cropper('getCroppedCanvas', {
                width: $('#dataWidth').val(),
                height: $('#dataHeight').val(),
            });//获取被裁剪后的canvas
            //var base64url = cas.toDataURL('image/png'); //转换为base64地址形式
            var base64url =  cas.toDataURL('image/jpeg', 0.8);  
            $("#finalImg").prop("src",base64url);//显示为图片的形式
            $("#tailoringImg").prop("src","")
            $("#tailoringImg").prop("src",base64url)
            //$("#idCardImage").val(base64url);
            imagesAjax(base64url);
            //关闭裁剪框
            // closeTailor();
        }
    });
    //关闭裁剪框
    function closeTailor() {
        $(".tailoring-container").toggle();
    }
    function imagesAjax(src) {
        var data = {};
        data.file = src;
        data.picurl = "<?php echo $picurl?>";//原图
        data.width = $('#dataWidth').val();
        data.height = $('#dataHeight').val();
        $.ajax({
            url: '<?php echo "index.php?m=attachment&c=attachments&a=crop_upload&module=$module&catid=$catid"?>',
            data: data,
            type: "POST",
            dataType: 'text',
            success: function(res) {
                console.log(res);
            	res = JSON.parse(res);
                uploadComplete(res.url)
                $("#tailoringImg").prop("src",res.url)
                if(res){
                	$("#tailoringImg").prop("src",res)
                }
            }
        });
    }
    function uploadComplete(pic)
	{				
		console.log("<?php echo $_GET['input']?>")
		console.log(pic)
		if(parent.document.getElementById('<?php echo $_GET['input']?>')) {
			var input = parent.document.getElementById('<?php echo $_GET['input']?>');
		} else {
			var input = parent.right.document.getElementById('<?php echo $_GET['input']?>');
		}
		<?php if (!empty($_GET['preview'])):?>
		if(parent.document.getElementById('<?php echo $_GET['preview']?>')) {
			var preview = parent.document.getElementById('<?php echo $_GET['preview']?>');
		} else {
		var preview = parent.right.document.getElementById('<?php echo $_GET['preview']?>');
		}
		<?php else:?>
		var preview = '';
		<?php endif;?>
		if(pic) {
			input.value = pic;
			if (preview) preview.src = pic;
		}
		window.top.art.dialog({id:'crop'}).close();
	}
</script>
</body>
</html>